(function() {

   var util = YAHOO.util;
   var Dom = util.Dom, Event = util.Event, CSS_PREFIX = "WireIt-";

/**
 * Visual module that contains terminals. The wires are updated when the module is dragged around.
 * @class WebUIContainer
 * @namespace WireIt
 * @constructor
 * @param {Object}   options      Configuration object (see options property)
 * @param {WireIt.Layer}   layer The WireIt.Layer (or subclass) instance that contains this container
 * @author Igor Lazarev
 */
WireIt.WebUIContainer = function(options, layer) {
	WireIt.WebUIContainer.superclass.constructor.call(this, options, layer);
	if (options.xtype=="Workflow.Container"){
		this.type = "Task";	
	}
	else if (options.xtype=="WireIt.WebUIInputContainer"){
		this.type = "input";
	}
	else if (options.xtype=="WireIt.WebUIOutputContainer"){
		this.type = "output";
	}
	else if (options.xtype=="WireIt.WebUIFormContainer"){
		this.type = "comment";
	}
	else if (options.xtype=="WireIt.ScriptContainer"){
		this.type = "script";
	}
	else if (options.xtype=="WireIt.JavascriptContainer"){
		this.type = "javascript";
	}
	else if (options.xtype=="WireIt.PythonContainer"){
		this.type = "python";
	}
	
	
	
}

YAHOO.extend(WireIt.WebUIContainer, WireIt.Container, {

   setUnready: function(){
		this.ddHandle.style.backgroundColor = "#696969";
		this.bodyEl.style.border ="2px solid #696969";
		this.state = "UNREADY";
   },
   
   setReady: function(){
		this.ddHandle.style.backgroundColor = "#3366CC";
		this.bodyEl.style.border ="2px solid #3366CC"
		this.state = "READY";
   },
   
   setRunning: function() {
		this.ddHandle.style.backgroundColor = "#00C5CD";
		this.bodyEl.style.border ="2px solid #00C5CD"
		this.state = "RUNNING";
   },
   
   setWaiting: function() {
		this.ddHandle.style.backgroundColor = "#3366CC";
		this.bodyEl.style.border ="2px solid #3366CC"
		this.state = "WAITING";
   },
   
   setDone: function() {
		this.ddHandle.style.backgroundColor = "#00AF33";
		this.bodyEl.style.border ="2px solid #00AF33"
		this.state = "DONE";
   },
   
   setFailed: function() {
		this.ddHandle.style.backgroundColor = "#CD0000";
		this.bodyEl.style.border ="2px solid #CD0000"
		this.state = "FAILED";
   },
   
   setCancelled: function() {
		this.ddHandle.style.backgroundColor = "#696969";
		this.bodyEl.style.border ="2px solid #696969"
		this.state = "CANCELLED";
   },


getConfig: function() {
 	 var obj = WireIt.WebUIContainer.superclass.getConfig.call(this);
		
	  // type of the container
      if(this.options.title) {
		 obj.type = this.options.title;
      }
	  
	  // state of the container
	  if (this.state) {
		obj.state = this.state;	  
	  }
		
	     return obj;	
},
   
onMouseDown: function() { //if the container is selected
	WireIt.WebUIContainer.superclass.onMouseDown.call(this);
	
	this.layer.focusContainer(this); //fire a 'container focused' event
      
},

onResize: function(event, args) {
      this.size = args[0];
      WireIt.sn(this.bodyEl, null, {width: (this.size[0]-14)+"px", height: (this.size[1]-44)+"px"});
}, 
   
removeFocus: function(){

	WireIt.WebUIContainer.superclass.removeFocus.call(this);
	this.initState();
  
},

render: function(){
	  // Create the element
      this.el = WireIt.cn('div', {className: this.options.className});
   
      if(this.options.width) {
         this.el.style.width = this.options.width+"px";
      }
      if(this.options.height) {
         this.el.style.height = this.options.height+"px";
      }
   
      // Adds a handler for mousedown so we can notice the layer
      Event.addListener(this.el, "mousedown", this.onMouseDown, this, true);
   
      if(this.options.ddHandle) {
         // Create the drag/drop handle
      	this.ddHandle = WireIt.cn('div', {className: this.options.ddHandleClassName});
      	this.el.appendChild(this.ddHandle);
      	
         // Set title
         if(this.options.title) {
            this.title = WireIt.cn('span', null, null, this.options.title);
            this.ddHandle.appendChild(this.title);
         }
         
         // Icon
         if (this.options.icon) {
            var iconCn = WireIt.cn('img', {src: this.options.icon, className: 'WireIt-Container-icon'});
            this.ddHandle.appendChild(iconCn);
         }

      }
   
      // Create the body element
      this.bodyEl = WireIt.cn('div', {className: "body"});
      this.el.appendChild(this.bodyEl);
   
      if(this.options.resizable) {
         // Create the resize handle
      	this.ddResizeHandle = WireIt.cn('div', {className: this.options.resizeHandleClassName} );
      	this.el.appendChild(this.ddResizeHandle);
      }
   
      if(this.options.close) {
         // Close button
         this.closeButton = WireIt.cn('div', {className: this.options.closeButtonClassName} );
         this.el.appendChild(this.closeButton);
         Event.addListener(this.closeButton, "click", this.onCloseButton, this, true);
      }
	  
	     this.collapseButton = WireIt.cn('div', {className: "WireIt-Container-collapsebutton"} );
         this.el.appendChild(this.collapseButton);
         Event.addListener(this.collapseButton, "click", this.onCollapseButton, this, true);
		 
		 this.expandButton = WireIt.cn('div', {className: "WireIt-Container-expandbutton"} );
   		 Event.addListener(this.expandButton, "click", this.onExpandButton, this, true);
		 
		 this.loadingFlag = WireIt.cn('div', {className: "WireIt-Container-loadingflag"} );
		 
      // Append to the layer element
      this.layer.el.appendChild(this.el);
   
   	// Set the position
   	this.el.style.left = this.options.position[0]+"px";
   	this.el.style.top = this.options.position[1]+"px";	
			
},

onCollapseButton: function(){
	this.el.removeChild(this.collapseButton);
	this.el.appendChild(this.expandButton);
	this.el.removeChild(this.bodyEl);
	this.ddHandle.className ="WireIt-Container-ddhandle-collapsed";
	if (this.size) {
		this.height = WireIt.getIntStyle(this.el, "height");
		YAHOO.util.Dom.setStyle(this.el, "height", 31 + "px");
	}
		
	this.redrawAllWires();
	this.el.removeChild(this.ddResizeHandle);
},

onExpandButton: function(){
	this.el.removeChild(this.expandButton);
	this.el.appendChild(this.collapseButton);
	this.el.appendChild(this.bodyEl);
	this.ddHandle.className = "WireIt-Container-ddhandle";
	if (this.size) {
		YAHOO.util.Dom.setStyle(this.el, "height", this.height + "px");
		
	}
	this.redrawAllWires();
	this.el.appendChild(this.ddResizeHandle);
},

initState: function(){

//override me

} 


   
});

})()